//
//  LogViewController.m
//  xingfugangwan
//
//  Created by rainday on 14/12/25.
//  Copyright (c) 2014年 rainday. All rights reserved.
//

#import "LogViewController.h"
#import <AFNetworking.h>
#import "NetIP.h"

@interface LogViewController ()
@property (nonatomic,weak) IBOutlet UIButton *logButton;
@property (nonatomic,weak) IBOutlet UITextField *username;
@property (nonatomic,weak) IBOutlet UITextField *password;


- (IBAction)doLogin:(id)sender;

@end

@implementation LogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _logButton.layer.cornerRadius = 5.0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (void)doLogin:(id)sender
{
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"username": _username.text,@"password":_password.text};
    
    [manager POST:[[NetIP shareNetAddress] getLoginURLString] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSLog(@"xxx");
        [userDefaults setObject:[responseObject objectForKey:@"token"] forKey:@"token"];
        [userDefaults setObject:[responseObject objectForKey:@"username"] forKey:@"username"];
        [userDefaults synchronize];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}
@end
